//
//  HudMgr.m
//  storeapp
//
//  Created by huangyi on 13-3-12.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "HudMgr.h"
#import "HudObj.h"
#import "toolbox.h"
#import "HudDialog.h"
#import "HudTask.h"
#import "HudToast.h"
#import "HudTextInput.h"

@interface HudMgr ()

@property(nonatomic,strong)UIWindow *hudWindow;
@property(nonatomic,strong)NSMutableArray *hudViewControllers;

@end

@implementation HudMgr

+(HudMgr*)shared{
    static HudMgr *singleton=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton=[[HudMgr alloc] init];
    });
    return singleton;
}

-(id)init{
    self=[super init];
    if (self) {
        self.hudViewControllers=[NSMutableArray array];
        self.hudWindow=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.hudWindow.windowLevel=UIWindowLevelNormal+100;
        self.queue=dispatch_queue_create("app-hud-queue", 0);
    }
    return self;
}

-(void)showHud:(HudObj*)hud{
    if ([self.hudViewControllers indexOfObject:hud]==NSNotFound) {
        [self.hudViewControllers addObject:hud];
    }else{
        NSCAssert(0, @"HUD already added: %i",[self.hudViewControllers indexOfObject:hud]);
    }
    [self presentHud];
}
-(void)hideHud{
    [self dismissHud];
}
-(void)unblockHud{
    [self dismissHud];
}

-(void)presentHud{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.hudWindow.rootViewController==nil) {
            if (self.hudWindow.hidden) {
                [self.hudWindow makeKeyAndVisible];
            }
            if (self.hudWindow.rootViewController==nil) {
                HudObj *hud=self.hudViewControllers.firstObject;
                [hud buildHud];
                self.hudWindow.rootViewController=hud;
                if (hud.dim) {
                    self.hudWindow.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5];
                }
                if (hud.showBlock) {
                    hud.showBlock();
                }
            }
        }
    });
}
-(void)dismissHud{
    dispatch_async(dispatch_get_main_queue(), ^{
        HudObj *hud=self.hudViewControllers.firstObject;
        if (hud.hideBlock) {
            hud.hideBlock();
        }
        [self.hudViewControllers removeObject:hud];
        self.hudWindow.rootViewController=nil;
        if (self.hudViewControllers.count) {
            [self presentHud];
        }else{
            self.hudWindow.hidden=YES;
            [self.rootStage makeKeyAndVisible];
        }
    });
}

@end

void SendToast(NSString *message){
    DDLogCInfo(@"%@",message);
    HudToast *toast=[HudToast hudWithText:message];
    [toast show];
}
void SendAlert(NSString *message){
    DDLogCError(@"%@",message);
    HudDialog *alert=[HudDialog hudWithText:message];
    [alert show];
}
void SendTask(NSString *message,dispatch_block_t code){
    DDLogCInfo(@"%@",message);
    HudTask *task=[HudTask hudWithText:message code:code];
    [task show];
}
void SendTextInput(NSString *message,void (^code)(id payload)){
    DDLogCInfo(@"%@",message);
    HudTextInput *text=[HudTextInput hudWithTitle:message andBlock:code];
    [text show];
}

